#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import logging
import wsgiref.handlers
import os
import os.path
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp

import urllib
from google.appengine.api import urlfetch

import data_input
import tech
import weather
import tianqi
import movie
import game
from view import view_taobaoA
from view import view_admob
from view import view_ninja
from general_db import GeneralDb

import xml.etree.ElementTree as ET
from db_weather import DbWeather

PAGESIZE = 20

class MainHandler(webapp.RequestHandler):
  def get(self):
    # get which page do you need
    pagestr = self.request.get("page")
    try:
      page = int(pagestr)
    except Exception, e:
      page = 0
      logging.info('error !!!!!!!!')
    logging.info('!!!!!page = %d' % page)
    generaldb_query = GeneralDb.all().order('-date_input')
    number = generaldb_query.count()
    next = page
    generaldbs = generaldb_query.fetch(PAGESIZE+1, page*PAGESIZE)
    if(len(generaldbs)==PAGESIZE+1):
      next = page+1
    generaldbs = generaldbs[:PAGESIZE]
    #self.response.out.write('Hello samhoo!')
    template_values = {
      'generaldbs': generaldbs,
      'number': number,
      'next': next,
    }
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

  def post(self):
    # get which page do you need
    pagestr = self.request.get("page")
    try:
      page = int(pagestr)
    except Exception, e:
      page = 0
      logging.info('error !!!!!!!!')
    logging.info('!!!!!page = %d' % page)
    generaldb_query = GeneralDb.all().order('-date_input')
    number = generaldb_query.count()
    next = page
    generaldbs = generaldb_query.fetch(PAGESIZE+1, page*PAGESIZE)
    if(len(generaldbs)==PAGESIZE+1):
      next = page+1
    generaldbs = generaldbs[:PAGESIZE]
    #self.response.out.write('Hello samhoo!')
    template_values = {
      'generaldbs': generaldbs,
      'number': number,
      'next': next,
    }
    path = os.path.join(os.path.dirname(__file__), 'movie.html')
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication([('/', MainHandler),
                                      ('/data_input', data_input.DataInput),
                                      ('/tech', tech.Tech),
                                      ('/movie', movie.Movie),
                                      ('/tianqi', tianqi.Tianqi),
                                      ('/tianqiYahoo', tianqi.TianqiYahoo),
                                      ('/tianqiYahooCity', tianqi.TianqiYahooCity),
                                      ('/game', game.Game),
                                      ('/admob', view_admob.Admob),
                                      ('/bargain', view_taobaoA.TaobaoA),
                                      ('/ninja', view_ninja.Ninja)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
